﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DLV.Data.Entities;
using DLV.FrameWork;
using DLV.Library.Utilities;

namespace DLV.BackEnd.Pages
{
    public partial class PriceCalculator : AdminPageBase
    {
        protected List<TravelGuideCashInfo> TravelGuideCashs = new List<TravelGuideCashInfo>();
        protected decimal TotalTravelGuideCashPrices = 0;

        protected List<PriceItemInfo> TicketAirs = new List<PriceItemInfo>();
        protected decimal TotalTicketAirPrices = 0;
        protected decimal TotalTicketAirPricesAdults = 0;
        protected decimal TotalTicketAirPricesChild = 0;

        protected List<PriceItemInfo> TicketCars = new List<PriceItemInfo>();
        protected decimal TotalTicketCarPrices = 0;

        protected List<PriceItemInfo> TicketStations = new List<PriceItemInfo>();
        protected decimal TotalTicketStationPrices = 0;
        protected decimal TotalTicketStationPricesAdults = 0;
        protected decimal TotalTicketStationPricesChild = 0;

        protected List<PriceItemInfo> Insurances = new List<PriceItemInfo>();
        protected decimal TotalInsurancePrices = 0;
        protected decimal TotalInsurancePricesAdults = 0;
        protected decimal TotalInsurancePricesChild = 0;

        protected List<VisaPriceInfo> Visas = new List<VisaPriceInfo>();
        protected decimal TotalVisaPrices = 0;
        protected decimal TotalVisaPricesAdults = 0;
        protected decimal TotalVisaPricesChild = 0;

        protected List<PriceItemInfo> Restaurants = new List<PriceItemInfo>();
        protected decimal TotalRestaurantPrices = 0;
        protected decimal TotalRestaurantPricesAdults = 0;
        protected decimal TotalRestaurantPricesChild = 0;
        // khách sạn
        protected List<StayInfo> Hotels = new List<StayInfo>();
        protected decimal TotalHotelPrices = 0;
        protected decimal TotalHotelPricesAdults = 0;
        protected decimal TotalHotelPricesChild = 0;

        protected List<CommissionInfo> Commissions = new List<CommissionInfo>();
        protected decimal TotalCommissionPrices = 0;
        protected decimal TotalCommissionPricesAdults = 0;
        protected decimal TotalCommissionPricesChild = 0;

        private List<TourFullInfo> _tourFullList = new List<TourFullInfo>();
        private readonly TourFullController _tourFullController = new TourFullController();
        protected TourInfo tourInfo = new TourInfo();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!String.IsNullOrEmpty(Request.QueryString["TourId"]))
            {
                _tourFullList = _tourFullController.GetTourFullByTour(int.Parse(Request.QueryString["TourId"]));
                tourInfo=new TourController().GetTour(int.Parse(Request.QueryString["TourId"]));
            }
            if (_tourFullList.Count == 0) return;

            RenderForm();
        }

        private void RenderForm()
        {
            var travelCashIds = _tourFullList.Where(tourFullInfo => !String.IsNullOrEmpty(tourFullInfo.TravelGuideCashs)).Aggregate("", (current, tourFullInfo) => current + (String.IsNullOrEmpty(current) ? tourFullInfo.TravelGuideCashs : "," + tourFullInfo.TravelGuideCashs));
            if (!String.IsNullOrEmpty(travelCashIds))
            {
                TravelGuideCashs = new TravelGuideCashController().ListTravelGuideCashByIds(travelCashIds);
                TotalTravelGuideCashPrices = TravelGuideCashs.Sum(p => p.TotalPrice);
            }

            var ticketIds = _tourFullList.Where(tourFullInfo => !String.IsNullOrEmpty(tourFullInfo.Tickets)).Aggregate("", (current, tourFullInfo) => current + (String.IsNullOrEmpty(current) ? tourFullInfo.Tickets : "," + tourFullInfo.Tickets));
            if (!String.IsNullOrEmpty(ticketIds))
            {
                var priceItems = new PriceItemController().ListPriceItemByIds(ticketIds);
                TicketAirs = priceItems.Where(p => p.PriceItemType == (int)Globals.PriceType.TicketAir).ToList();
                TotalTicketAirPrices = TicketAirs.Sum(p => p.TotalPrice);
                TotalTicketAirPricesAdults = TicketAirs.Where(p=>p.AgeId == (int)Globals.Age.Adults).Sum(p => p.TotalPrice);
                TotalTicketAirPricesChild = TicketAirs.Where(p => p.AgeId == (int)Globals.Age.Child).Sum(p => p.TotalPrice);

                TicketCars = priceItems.Where(p => p.PriceItemType == (int)Globals.PriceType.TicketCar).ToList();
                TotalTicketCarPrices = TicketCars.Sum(p => p.TotalPrice);

                TicketStations = priceItems.Where(p => p.PriceItemType == (int)Globals.PriceType.TicketStation).ToList();
                TotalTicketStationPrices = TicketStations.Sum(p => p.TotalPrice);
                TotalTicketStationPricesAdults = TicketStations.Where(p => p.AgeId == (int)Globals.Age.Adults).Sum(p => p.TotalPrice);
                TotalTicketStationPricesChild = TicketStations.Where(p => p.AgeId == (int)Globals.Age.Child).Sum(p => p.TotalPrice);
            }

            var insurances = _tourFullList.Where(tourFullInfo => !String.IsNullOrEmpty(tourFullInfo.Insurance)).Aggregate("", (current, tourFullInfo) => current + (String.IsNullOrEmpty(current) ? tourFullInfo.Insurance : "," + tourFullInfo.Insurance));
            if (!String.IsNullOrEmpty(insurances))
            {
                var priceItems = new PriceItemController().ListPriceItemByIds(insurances);
                Insurances = priceItems.Where(p => p.PriceItemType == (int)Globals.PriceType.Insurance).ToList();
                TotalInsurancePrices = Insurances.Sum(p => p.TotalPrice);
                TotalInsurancePricesAdults = Insurances.Where(p => p.AgeId == (int)Globals.Age.Adults).Sum(p => p.TotalPrice);
                TotalInsurancePricesChild = Insurances.Where(p => p.AgeId == (int)Globals.Age.Child).Sum(p => p.TotalPrice);
            }

            var visas = _tourFullList.Where(tourFullInfo => !String.IsNullOrEmpty(tourFullInfo.Visas)).Aggregate("", (current, tourFullInfo) => current + (String.IsNullOrEmpty(current) ? tourFullInfo.Visas : "," + tourFullInfo.Visas));
            if (!String.IsNullOrEmpty(visas))
            {
                Visas = new VisaPriceController().ListVisaPriceByIds(visas);
                TotalVisaPrices = Visas.Sum(p => p.TotalPrice);
                TotalVisaPricesAdults = Visas.Where(p => p.AgeId == (int)Globals.Age.Adults).Sum(p => p.TotalPrice);
                TotalVisaPricesChild = Visas.Where(p => p.AgeId == (int)Globals.Age.Child).Sum(p => p.TotalPrice);
            }

            var restaurants = _tourFullList.Where(tourFullInfo => !String.IsNullOrEmpty(tourFullInfo.Restaurant)).Aggregate("", (current, tourFullInfo) => current + (String.IsNullOrEmpty(current) ? tourFullInfo.Restaurant : "," + tourFullInfo.Restaurant));
            if (!String.IsNullOrEmpty(restaurants))
            {
                Restaurants = new PriceItemController().ListPriceItemByIds(restaurants);
                TotalRestaurantPrices = Restaurants.Sum(p => p.TotalPrice);
                TotalRestaurantPricesAdults = Restaurants.Where(p => p.AgeId == (int)Globals.Age.Adults).Sum(p => p.TotalPrice);
                TotalRestaurantPricesChild = Restaurants.Where(p => p.AgeId == (int)Globals.Age.Child).Sum(p => p.TotalPrice);
            }
            //khách sạn
            var hotels = _tourFullList.Where(tourFullInfo => !String.IsNullOrEmpty(tourFullInfo.Hotels)).Aggregate("", (current, tourFullInfo) => current + (String.IsNullOrEmpty(current) ? tourFullInfo.Hotels : "," + tourFullInfo.Hotels));
            if (!String.IsNullOrEmpty(hotels))
            {
                Hotels = new StayController().ListStayByIds(hotels);
                TotalHotelPrices = Hotels.Sum(p => p.TotalPrice);
                TotalHotelPricesAdults = Hotels.Where(p => p.AgeId == (int)Globals.Age.Adults).Sum(p => p.TotalPrice);
                TotalHotelPricesChild = Hotels.Where(p => p.AgeId == (int)Globals.Age.Child).Sum(p => p.TotalPrice);
            }
          
        }
    }
}
